const requireEx=require('yi-require');
const aggregate = require('yi-aggregate');

middleware =  (req, res, next) =>{
    let doc=req.body;
    req.mongo.collection('warehouse').find({goodsId:doc.goodsId,storeId:doc.storeId}).next((e,r)=>{
        if(!r||r.length===0){
            req.mongo.collection('warehouse').insertOne({goodsId:doc.goodsId,storeId:doc.storeId,goods_store:doc.goodsId.toString()+'_'+doc.storeId,quantity:doc.quantity,createTime:new Date()},(e0,r0)=>{

            });
        }else{
            req.mongo.collection('warehouse').updateOne({goodsId:doc.goodsId,storeId:doc.storeId},{$inc:{quantity:doc.quantity}},(e0,r0)=>{

            });
        }
        next();
    });

};

module.exports = {
    open:true,
    description:'获取仓库商品列表',
    extend:"",
    middleware:middleware,
    handler: requireEx('yi-restful').c('stock',[])
};
